//
//  PropertyDetailsViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 10/26/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "PropertyDetailsViewController.h"
#import "ListOptionsViewController.h"

@interface PropertyDetailsViewController () <ListOptionsDelegate, SmarthingsDelegate>

@property (weak, nonatomic) IBOutlet UITableViewCell *propertyTypeCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *descriptionCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *unitCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *minCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *maxCell;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (strong, nonatomic) Smarthings *smarthingsSDK;
@property (strong, nonatomic) NSTimer * refreshTimer;

- (void)refreshPropertyValue;

@end

@implementation PropertyDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.smarthingsSDK = [Smarthings new];
    self.smarthingsSDK.delegate = self;
    if (self.isNewThing) {
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped)];
        self.navigationItem.leftBarButtonItem = cancelButton;
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped)];
        self.navigationItem.rightBarButtonItem = doneButton;
        self.propertyTypeCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.propertyTypeCell.userInteractionEnabled = YES;
        self.valueLabel.hidden = YES;
    }
    else {
        self.propertyTypeCell.accessoryType = UITableViewCellAccessoryNone;
        self.propertyTypeCell.userInteractionEnabled = NO;
        self.valueLabel.hidden = NO;
        self.valueLabel.text = @"";
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.propertyTypeCell.detailTextLabel.text = self.property.propertyType.uuid;
    self.descriptionCell.detailTextLabel.text = self.property.propertyType.name;
    if (self.property.propertyType.unit) {
        self.unitCell.detailTextLabel.text = self.property.propertyType.unit;
        self.unitCell.hidden = NO;
    }
    else
    {
        self.unitCell.hidden = YES;
    }
    if (self.property.propertyType.min) {
        self.minCell.detailTextLabel.text = self.property.propertyType.min.description;
        self.minCell.hidden = NO;
    }
    else
    {
        self.minCell.hidden = YES;
    }
    if (self.property.propertyType.max) {
        self.maxCell.detailTextLabel.text = self.property.propertyType.max.description;
        self.maxCell.hidden = NO;
    }
    else
    {
        self.maxCell.hidden = YES;
    }
    self.refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                         target:self
                                                       selector:@selector(refreshPropertyValue)
                                                       userInfo:nil
                                                        repeats:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.refreshTimer invalidate];
    self.refreshTimer = nil;
}

- (void)refreshPropertyValue {
    [self.smarthingsSDK getPropertyValue:self.property];
}

- (void)cancelButtonTapped {
    [self.propertiesArray removeObject:self.property];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)doneButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SelectPropertyType"]) {
        [(ListOptionsViewController *)[segue destinationViewController] setDelegate:self];
        [(ListOptionsViewController *)[segue destinationViewController] setList:[Smarthings thingPropertyTypes]];
        [(ListOptionsViewController *)[segue destinationViewController] setSelectedIndex:[[Smarthings thingPropertyTypes] indexOfObject:self.property.propertyType]];
        [[(ListOptionsViewController *)[segue destinationViewController] navigationItem] setTitle:@"Select Property Type"];
    }
}

- (void)indexHasBeenChosen:(NSInteger)index {
    self.property.propertyType = [Smarthings thingPropertyTypes][index];
    self.property.name = self.property.propertyType.uuid;
}

#pragma mark - Smarthings Delegate

- (void)getPropertyValueFinishedWithFailure:(NSError *)error {
    self.valueLabel.text = @"Not available  ";
}

- (void)getPropertyValueFinishedWithSuccess:(SMTThingProperty *)property forValue:(NSNumber *)value {
    if (self.property.propertyType.unit) {
        self.valueLabel.text = [NSString stringWithFormat:@"%@ %@", value.description, self.property.propertyType.unit];
    }
    else {
        if ([self.property.name isEqualToString:@"onoff"]) {
            self.valueLabel.text = value.boolValue ? @"ON" : @"OFF";
        }
        else if ([self.property.propertyType.valueType isEqualToString:@"BOOLEAN"])
        {
            self.valueLabel.text = value.boolValue ? @"YES" : @"NO";
        }
        else {
            self.valueLabel.text = value.description;
        }
    }
}

@end
